package com.good.child.pressent;

import android.content.Context;
import android.text.TextUtils;

import com.good.child.bean.login.LoginBean;
import com.good.child.bean.login.UserInfoBean;
import com.good.child.config.Constants;
import com.good.child.http.LoginLogical;
import com.good.child.iview.LoginIView;
import com.google.gson.Gson;
import com.yimidida.library.base.BasePresenter;
import com.yimidida.library.base.ICallBackResultListener;
import com.yimidida.library.base.LibraryApplication;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import butterknife.Action;
import rx.functions.Action1;

/**
 * Create on: 2024/2/24
 * by Mr.Zhu
 * Description: 登录presenter
 */
public class LoginPresenter extends BasePresenter<LoginIView> {

    private Context mContext;

    public LoginPresenter(Context mContext) {
        this.mContext = mContext;
    }

    public void getLogin(String telephone, String vercode) {
        LoginLogical.getInstance().requestUserLogin(mContext, telephone, vercode, new ICallBackResultListener() {
            @Override
            public void onCallBack(Object result) {
                if (result == null) {
                    return;
                }
                LoginBean userInfo = (LoginBean) result;
                if (userInfo == null) {
                    return;
                }
                LibraryApplication.getSharedata().saveData(Constants.LOGIN.TOKEN_ID, userInfo.getAccess_token());
                Constants.token = userInfo.getAccess_token();
                getUserInfo(mContext);
            }
        });
    }

    public void getVerifyCode(String telephone) {
        LoginLogical.getInstance().requestVerifyCode(mContext, telephone, new ICallBackResultListener() {
            @Override
            public void onCallBack(Object result) {
            }
        });
    }

    public void getUserInfo(Context context) {

        LoginLogical.getInstance().getUserInfo(context, Constants.deviceCode, new ICallBackResultListener() {
            @Override
            public void onCallBack(Object result) {
                if (result == null) {
                    return;
                }
                UserInfoBean userInfo = (UserInfoBean) result;
                if (userInfo == null) {
                    return;
                }
                LibraryApplication.getSharedata().saveData(Constants.LOGIN_TELEPHONE, userInfo.getTelephone());
                LibraryApplication.getSharedata().saveData(Constants.DEVICE_CODE, userInfo.getDeviceCode());
                if (userInfo.getUserName()!=null) {
                    LibraryApplication.getSharedata().saveData(Constants.USER_NAME, userInfo.getUserName());
                }
                if (userInfo.getAvatar()!=null) {
                    LibraryApplication.getSharedata().saveData(Constants.USER_HEADER, userInfo.getAvatar());
                }
                mView.onUserLoginSuccess();
            }
        });
    }

}
